import {
  getOrientation,
  getStageAndCourseByOrientationId
} from "@/api/course/index";
import { ref, onMounted } from "vue";

export function useCourseFindHooks() {
  const orientationList = ref<any>([]);

  const stageList = ref<any>([]);
  const currentOrientationId = ref("");

  const currentCourseList = ref<any>([]);

  const currentOrientationIndex = ref(0);
  const currentStageIndex = ref(0);

  async function getOrientationFun() {
    const res = await getOrientation();

    orientationList.value = res.data?.records;
    currentOrientationId.value = res.data?.records[0]?.id;
    getStageFun();
  }

  async function getStageFun() {
    const res = await getStageAndCourseByOrientationId({
      id: currentOrientationId.value
    });

    stageList.value = res.data;

    currentCourseList.value = stageList.value[0]?.coursesList;
  }

  function orientationClick(ite: any, i: number) {
    currentOrientationIndex.value = i;
    currentOrientationId.value = ite.id;
    getStageFun();
  }

  function stageClick(ite: any, i: number) {
    currentStageIndex.value = i;
    currentCourseList.value = ite.coursesList;
  }

  onMounted(() => {
    getOrientationFun();
  });

  return {
    currentOrientationIndex,
    currentStageIndex,
    orientationList,
    stageList,
    currentCourseList,
    stageClick,
    orientationClick
  };
}
